package cn.xm.org.controller;

import cn.xm.basic.result.AjaxResult;
import cn.xm.basic.result.PageList;
import cn.xm.org.domain.ShopAuditLog;
import cn.xm.org.service.ShopService;
import cn.xm.org.domain.Shop;
import cn.xm.org.query.ShopQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public ShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        if( shop.getId()!=null){
            shopService.update(shop);
        } else {
            shopService.add(shop);
        }
        return AjaxResult.me();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        shopService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){
        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryByPage(query);
    }

    /**
     * 审核通过
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog)
    {
        shopService.auditPass(shopAuditLog);
        return AjaxResult.me();
    }

    @PostMapping("/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog)
    {
        shopService.auditReject(shopAuditLog);
        return AjaxResult.me();
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settle")
    public AjaxResult settle(@RequestBody @Valid Shop shop)
    {
        shopService.settle(shop);
        return AjaxResult.me();
    }

    @GetMapping("/active/{id}")
    public AjaxResult activate(@PathVariable("id") Long id)
    {
        shopService.activate(id);
        return AjaxResult.me();
    }

    @GetMapping("/echarts")
    public AjaxResult echarts()
    {
        return shopService.echarts();
    }


}
